/************************************************************************************/
/*  Program name CAPFAIMP_BOOK_CTL.sql                                        		*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 5 JAN, 2006   NIP Karen         First created                               		*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER IGI_MHC_FA_BOOK_CONTROLS_T1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_BOOK_CTL';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_BOOK_CONTROLS;
	

BEGIN
	UPDATE
		FA_BOOK_CONTROLS
	SET	
		BOOK_TYPE_CODE = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE),
		BOOK_TYPE_NAME = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_NAME,1,4), BOOK_TYPE_NAME)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_BOOK_CONTROLS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_BOOK_CONTROLS
			VALUES
			(
			C1.BOOK_TYPE_CODE,
			C1.BOOK_TYPE_NAME,
			CAPBRIMP_COMMON.GetSOBID(C1.SET_OF_BOOKS_ID_NAME),
			C1.INITIAL_DATE,
			C1.LAST_DEPRN_RUN_DATE,
			C1.AMORTIZE_FLAG,
			C1.FULLY_RESERVED_FLAG,
			C1.DEPRN_CALENDAR,
			C1.BOOK_CLASS,
			C1.GL_POSTING_ALLOWED_FLAG,
			C1.CURRENT_FISCAL_YEAR,
			C1.ALLOW_MASS_CHANGES,
			C1.ALLOW_DEPRN_ADJUSTMENTS,
			CAPBRIMP_COMMON.GetCOAID(C1.SET_OF_BOOKS_ID_NAME),
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.PRORATE_CALENDAR,
			C1.DATE_INEFFECTIVE,
			C1.JE_RETIREMENT_CATEGORY,
			C1.JE_DEPRECIATION_CATEGORY,
			C1.JE_RECLASS_CATEGORY,
			C1.GL_JE_SOURCE,
			C1.JE_ADDITION_CATEGORY,
			C1.JE_ADJUSTMENT_CATEGORY,
			C1.DISTRIBUTION_SOURCE_BOOK,
			C1.JE_TRANSFER_CATEGORY,
			C1.COPY_RETIREMENTS_FLAG,
			C1.COPY_ADJUSTMENTS_FLAG,
			C1.DEPRN_REQUEST_ID,
			C1.ALLOW_COST_CEILING,
			C1.ALLOW_DEPRN_EXP_CEILING,
			C1.CALCULATE_NBV,
			C1.RUN_YEAR_END_PROGRAM,
			C1.JE_DEFERRED_DEPRN_CATEGORY,
			C1.ALLOW_CIP_ASSETS_FLAG,
			C1.ITC_ALLOWED_FLAG,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.ALLOW_MASS_COPY,
			C1.ALLOW_PURGE_FLAG,
			C1.ALLOW_REVAL_FLAG,
			C1.AMORTIZE_REVAL_RESERVE_FLAG,
			C1.AP_INTERCOMPANY_ACCT,
			C1.AR_INTERCOMPANY_ACCT,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.CAPITAL_GAIN_THRESHOLD,
			C1.COPY_SALVAGE_VALUE_FLAG,
			C1.COST_OF_REMOVAL_CLEARING_ACCT,
			C1.COST_OF_REMOVAL_GAIN_ACCT,
			C1.COST_OF_REMOVAL_LOSS_ACCT,
			C1.DEFAULT_LIFE_EXTENSION_CEILING,
			C1.DEFAULT_LIFE_EXTENSION_FACTOR,
			C1.DEFAULT_MAX_FULLY_RSVD_REVALS,
			C1.DEFAULT_REVAL_FULLY_RSVD_FLAG,
			C1.DEFERRED_DEPRN_EXPENSE_ACCT,
			C1.DEFERRED_DEPRN_RESERVE_ACCT,
			C1.DEPRN_ALLOCATION_CODE,
			C1.DEPRN_STATUS,
			C1.FISCAL_YEAR_NAME,
			C1.INITIAL_PERIOD_COUNTER,
			C1.JE_CIP_ADJUSTMENT_CATEGORY,
			C1.JE_CIP_ADDITION_CATEGORY,
			C1.JE_CIP_RECLASS_CATEGORY,
			C1.JE_CIP_RETIREMENT_CATEGORY,
			C1.JE_CIP_REVAL_CATEGORY,
			C1.JE_CIP_TRANSFER_CATEGORY,
			C1.JE_REVAL_CATEGORY,
			C1.LAST_MASS_COPY_PERIOD_COUNTER,
			C1.LAST_PERIOD_COUNTER,
			C1.LAST_PURGE_PERIOD_COUNTER,
			C1.MASS_COPY_SOURCE_BOOK,
			C1.MASS_REQUEST_ID,
			C1.NBV_AMOUNT_THRESHOLD,
			C1.NBV_FRACTION_THRESHOLD,
			C1.NBV_RETIRED_GAIN_ACCT,
			C1.NBV_RETIRED_LOSS_ACCT,
			C1.PROCEEDS_OF_SALE_CLEARING_ACCT,
			C1.PROCEEDS_OF_SALE_GAIN_ACCT,
			C1.PROCEEDS_OF_SALE_LOSS_ACCT,
			C1.REVALUE_ON_RETIREMENT_FLAG,
			C1.REVAL_DEPRN_RESERVE_FLAG,
			C1.REVAL_POSTING_FLAG,
			C1.REVAL_RSV_RETIRED_GAIN_ACCT,
			C1.REVAL_RSV_RETIRED_LOSS_ACCT,
			C1.DEPRN_ADJUSTMENT_ACCT,
			C1.IMMEDIATE_COPY_FLAG,
			C1.JE_DEPRN_ADJUSTMENT_CATEGORY,
			C1.DEPR_FIRST_YEAR_RET_FLAG,
			CAPBRIMP_COMMON.GetSegment(CAPBRIMP_COMMON.GetCOAID(C1.SET_OF_BOOKS_ID_NAME),C1.FLEXBUILDER_DEFAULTS_CCID_D),
			C1.RETIRE_REVAL_RESERVE_FLAG,
			C1.USE_CURRENT_NBV_FOR_DEPRN,
			C1.COPY_ADDITIONS_FLAG,
			C1.USE_PERCENT_SALVAGE_VALUE_FLAG,
			C1.MC_SOURCE_FLAG,
			C1.REVAL_YTD_DEPRN_FLAG,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER IGI_MHC_FA_BOOK_CONTROLS_T1 ENABLE;